jQuery.fn.modal = function() {
	return this.each(function(){
	var ah = $(this).height();
        var wh = $(window).height();
        var nh = (wh/2) - (ah/2);
 
        var aw = $(this).width();
        var ww = $(window).width();
        var nw = (ww/2) - (aw/2);
 
        // Trim body height, append overlay, and disable scrolling (this can be undone with a custom close button)
        $("body").height(wh).css('overflow','hidden').scrollTop(0).append('<div class="overlay"><\/div>');
        $(".overlay").fadeTo("slow", .8);
        $(this).css({'top':nh, 'left':nw}).fadeIn("slow");
	});
};

$(document).ready(function(){
	$('a[rel="modal"]').click(function(){
		window.scroll(0,0);	
		
		url = $(this).attr('href');
		
		// If this is an image, we need to append, rather than load
 	 	if(url.match('jpg|gif|png|jpeg')){
 	 		$('#modal .inner').append('<img src="'+url+'" alt="Large View" />');
 	 		$('#modal').modal();
 	 	}
		else { 
			$('#modal .inner').load(url);
			$('#modal').modal();
		}	
		
		return false;
	});
	
	$('#nav-main .sub ul li ul li').prepend('<span>-</span>');
	
	$('#nav-main li').hover(
		function(){
			$(this).children('div').show();
		},
		function(){
			$(this).children('div').hide();
		}
	);
	
	$('.modal').click(function(){
		window.scroll(0,0);	
		$('#modal').modal();
		if($(this).attr('rel')=='external'){
			// If it's a youtube video, grab iframe embed
			if($(this).attr('data-key')=='youtube'){ 
				$('#modal .inner').append('<iframe title="YouTube video player" class="youtube-player" type="text/html" width="600" height="366" src="http://www.youtube.com/embed/'+$(this).attr('data-value')+'" frameborder="0"></iframe>');
				$("#modal").css('min-height',416);
			}
			
			// If it's a vimeo video, grab iframe embed
			if($(this).attr('data-key')=='vimeo'){ 
				$('#modal .inner').append('<iframe src="http://player.vimeo.com/video/'+$(this).attr('data-value')+'" width="600" height="366" frameborder="0"></iframe>');
				$("#modal").css('min-height',416);
			}
		}
		
		return false;
	});
	
	$('#newsletter-view').click(function(){
		window.scroll(0,0);	
		$('#newsletter-form').modal();
		
		return false;
	});
	
	$('#contact-view').click(function(){
		window.scroll(0,0);	
		$('#contact-form').modal();
		
		return false;
	});
	
	$('#atc-view').click(function(){
		window.scroll(0,0);	
		$('#atc-form').modal();
		
		return false;
	});
	
	$(".modal-close, .overlay").click(function(){
		$("#modal, #newsletter-form, #contact-form, #atc-form").hide();
		$(".overlay").remove();
		$("#modal .inner iframe, #modal .inner img, #modal .inner *").remove();
		$("body").css({ 'height':'auto', 'overflow':'visible' });
	});
	
	$('#language, #LangDropdown').hover(
		function(){ $('#language, #LangDropdown').addClass('menuOpen'); },
		function(){ $('#language, #LangDropdown').removeClass('menuOpen'); }
	);
	$('#search, #SearchDropdown').hover(
		function(){ $('#search, #SearchDropdown').addClass('menuOpen'); },
		function(){ $('#search, #SearchDropdown').removeClass('menuOpen'); }
	);
});